package com.bole.controller;


import com.bole.dao.CarSpecDetailsDao;
import com.bole.dto.specdetails.TbCarSpecDetailsDto;
import com.bole.dto.user.DicCarSpecDto;
import com.bole.pojo.DicCarSpecDo;
import com.bole.pojo.TbCarSpecDetailsDo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("details")
@Api(value="规格详情接口")
public class CarSpecDetailsController {

    @Autowired
    private CarSpecDetailsDao carSpecDetailsDao;

    //查询

    @GetMapping("getSpecDetailsInfo")
    @ApiOperation(value = "查询规格详情")
    public List<TbCarSpecDetailsDto> getSpecInfo() {
        List<TbCarSpecDetailsDo> all = carSpecDetailsDao.findAll();
        ArrayList<TbCarSpecDetailsDto> carSpecDetailsDtoArrayList = new ArrayList<>();
        for (TbCarSpecDetailsDo carSpecDetailsDo : all) {
            TbCarSpecDetailsDto carSpecDetailsDto = new TbCarSpecDetailsDto();
            BeanUtils.copyProperties(carSpecDetailsDo, carSpecDetailsDto);
            carSpecDetailsDtoArrayList.add(carSpecDetailsDto);
        }
        return carSpecDetailsDtoArrayList;

    }

//增加

    @PostMapping("saveSpecDetailsInfo")
    @ApiOperation(value = "保存规格详情")
    @ApiImplicitParam(name = "carSpecDetailsDo",value = "规格详情",required = true,dataTypeClass =TbCarSpecDetailsDo.class)
    public void saveContentCateInfo(@RequestBody TbCarSpecDetailsDo carSpecDetailsDo) {
        TbCarSpecDetailsDo  specDetailsDo = carSpecDetailsDao.save(carSpecDetailsDo);
        TbCarSpecDetailsDto carSpecDetailsDto = new TbCarSpecDetailsDto();
        BeanUtils.copyProperties(specDetailsDo,carSpecDetailsDto);
    }
//回显

    @PostMapping("toUpdate")
    @ApiOperation(value = "回显")
    @ApiImplicitParam(name = "id",value = "规格详情id",required = true,dataTypeClass = Long.class)
    public TbCarSpecDetailsDto toUpdate(@RequestParam("id") Long id) {
        TbCarSpecDetailsDo carSpecDetailsDo = carSpecDetailsDao.findById(id).get();
       TbCarSpecDetailsDto carSpecDetailsDto = new TbCarSpecDetailsDto();
        BeanUtils.copyProperties(carSpecDetailsDo,carSpecDetailsDto);
        return carSpecDetailsDto;
    }
    //修改

    @PostMapping("updateSpecDetailsInfo")
    @ApiOperation(value = "修改规格")
    @ApiImplicitParam(name = "carSpecDetailsDo",value = "规格",required = true,dataTypeClass = TbCarSpecDetailsDo.class)
    public void updateSpecInfo(@RequestBody TbCarSpecDetailsDo carSpecDetailsDo) {
        TbCarSpecDetailsDo specDetailsDo = carSpecDetailsDao.save(carSpecDetailsDo);
        TbCarSpecDetailsDto carSpecDetailsDto = new TbCarSpecDetailsDto();
        BeanUtils.copyProperties(specDetailsDo, carSpecDetailsDto);
    }

//删除
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除")
    @ApiImplicitParam(name = "ides",value = "规格",required = true,dataTypeClass = Long.class)
    public void deleteBatch(@RequestParam("ides") List<Long> ides) {

        carSpecDetailsDao.deleteBatch(ides);
    }

    //通过规格id查询
    @PostMapping("selectBySpecId")
    @ApiOperation(value = "回显")
    @ApiImplicitParam(name = "id",value = "规格id",required = true,dataTypeClass = Long.class)
    public List<TbCarSpecDetailsDto> selectBySpecId(@RequestParam("specId") Long specId) {
        List<TbCarSpecDetailsDo> dicCarSpecDoList = carSpecDetailsDao.findAllBySpecId(specId);
        ArrayList<TbCarSpecDetailsDto> dicCarSpecDtos = new ArrayList<>();
        for (TbCarSpecDetailsDo carSpecDo : dicCarSpecDoList) {
            TbCarSpecDetailsDto dicCarSpecDto = new TbCarSpecDetailsDto();
            BeanUtils.copyProperties(carSpecDo, dicCarSpecDto);
            dicCarSpecDtos.add(dicCarSpecDto);
        }
        return dicCarSpecDtos;
    }

}
